﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class TransferDetailBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public TransferDetailBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string TransferID, string ProductDetail)
        {
            if (DAO.T_TRANSFER_DETAILs.Where(t => t.TRANSFER_ID.ToString() == TransferID 
                                                && t.PRODUCT_DETAIL_ID.ToString() == ProductDetail).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_TRANSFER_DETAIL> GetTransferDetailList()
        {
            var list = DAO.T_TRANSFER_DETAILs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_TRANSFER_DETAIL GetTransferDetailByID(string ID)
        {
            return DAO.T_TRANSFER_DETAILs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddTransferDetail(T_TRANSFER_DETAIL TransferDetail)
        {
            try
            {
                TransferDetail.ID = DAO.GetGUID();

                DAO.T_TRANSFER_DETAILs.InsertOnSubmit(TransferDetail);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddTransferDetail(T_TRANSFER_DETAIL TransferDetail, out string TransferDetailGuid)
        {
            try
            {
                TransferDetail.ID = DAO.GetGUID();
                
                DAO.T_TRANSFER_DETAILs.InsertOnSubmit(TransferDetail);
                DAO.SubmitChanges();

                TransferDetailGuid = TransferDetail.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                TransferDetailGuid = "";
                return -1;
            }
        }

        public int UpdateTransferDetail(T_TRANSFER_DETAIL TransferDetail)
        {
            try
            {
                T_TRANSFER_DETAIL updateEntity = DAO.T_TRANSFER_DETAILs.SingleOrDefault(t=>t.ID.ToString()==TransferDetail.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.PRODUCT_DETAIL_ID = TransferDetail.PRODUCT_DETAIL_ID;
                updateEntity.FROM_FLOOR_ID = TransferDetail.FROM_FLOOR_ID;
                updateEntity.GROSS_WEIGHT = TransferDetail.GROSS_WEIGHT;
                updateEntity.NET_WEIGHT = TransferDetail.NET_WEIGHT;
                updateEntity.PACKET_QTY = TransferDetail.PACKET_QTY;
                updateEntity.PACKET_QTY2 = TransferDetail.PACKET_QTY2;
                updateEntity.REMARK = TransferDetail.REMARK;
                updateEntity.TO_FLOOR_ID = TransferDetail.TO_FLOOR_ID;
                updateEntity.TRANSFER_ID = TransferDetail.TRANSFER_ID;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteTransferDetail(string ID)
        {
            try
            {
                T_TRANSFER_DETAIL deleteEntity = DAO.T_TRANSFER_DETAILs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_TRANSFER_DETAILs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteTransferDetails(List<string> IDs)
        {
            try
            {
                foreach (string ID in IDs)
                {
                    T_TRANSFER_DETAIL deleteEntity = DAO.T_TRANSFER_DETAILs.SingleOrDefault(t => t.ID.ToString() == ID);
                    if (deleteEntity == null)
                        return -1;
                    DAO.T_TRANSFER_DETAILs.DeleteOnSubmit(deleteEntity);
                }

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
